import { createApp } from "vue";
import App from "./App.vue";
import { createPinia } from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
import setComponents from "./components";
import i18n,{changeLanguage } from "./i18n";
import uviewPlus from "uview-plus";
import "uview-plus/index.scss";
import { useStore } from "@/stores";


// #ifdef H5
// import vConsole from "vconsole";
// new vConsole();
// #endif

import {
	formattingTime,
	goTo,
	goToTab,
	goBack,
	goReLaunch,
	goRedirectTo,
	copyText,
	getLastFourChars,
	handleDownload,
	validateEmail,
	getTimestampToMidnight,
	interceptFn
} from "@/static/ts/common";

import { getRsaCode } from "@/static/ts/rsa";

// #ifdef H5
import IndexedDB from "../db/indexedDB";
// #endif
// #ifdef APP-PLUS
import SQLite from "../db/sqlite";
// #endif

// 创建 pinia 实例
const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);

// 数据库初始化函数
async function initializeDatabase() {
	let db : any;
	try {
		// #ifdef APP-PLUS
		db = new SQLite("myChatApp");
		await db.init();
		console.log("SQLite 数据库初始化成功");
		await db.createTables();
		console.log("SQLite 表创建成功");
		// #endif

		// #ifdef H5
		db = new IndexedDB("myChatApp");
		await db.init();
		console.log("IndexedDB 数据库初始化成功");
		// #endif

		// 返回数据库实例
		return db;
	} catch (error) {
		console.error("数据库初始化失败:", error);
		return null;
	}
}

// 启动应用的方法
async function startApp() {
	// const db = await initializeDatabase();
	if (true) {
		const app = createApp(App);

		// 使用插件和全局方法
		app.use(pinia);
		app.use(i18n);
		app.use(uviewPlus);

		app.config.globalProperties.formattingTime = formattingTime;
		app.config.globalProperties.getRsaCode = getRsaCode;

		// 跳转方法注入
		app.config.globalProperties.goTo = goTo;
		app.config.globalProperties.goToTab = goToTab;
		app.config.globalProperties.goBack = goBack;
		app.config.globalProperties.goReLaunch = goReLaunch;
		app.config.globalProperties.goRedirectTo = goRedirectTo;
		app.config.globalProperties.copyText = copyText;
		app.config.globalProperties.getLastFourChars = getLastFourChars;
		app.config.globalProperties.handleDownload = handleDownload;
		app.config.globalProperties.validateEmail = validateEmail;
		app.config.globalProperties.getTimestampToMidnight = getTimestampToMidnight;
		app.config.globalProperties.interceptFn = interceptFn;
		app.config.globalProperties.$changeLanguage = changeLanguage;

		// 注册组件
		setComponents(app);

		// 注入数据库实例到全局属性
		// app.config.globalProperties.$db = db;

		// 设置数据库到 Pinia store
		const store = useStore();
		// store.setDb(db);
		app.directive('number-only', {
		  mounted(el: HTMLElement, binding, vnode:any) {
		    // 获取自定义组件的输入框元素
		    const input = el.querySelector('input');
		    if (input) {
		      input.addEventListener('input', (event: Event) => {
		        const target = event.target as HTMLInputElement;
		        // 过滤非数字字符
		        target.value = target.value.replace(/[^\d]/g, '');
		
		        // 手动触发 v-model 更新
		        if (vnode.componentInstance) {
		          vnode.componentInstance.$emit('input', target.value);
		        }
		      });
		    }
		  },
		});


		// 挂载应用
		app.mount("#app");


	} else {
		console.error("数据库初始化失败，无法启动应用！");
	}
}

// 初始化并启动应用
startApp();